#include "link_queue.h"

// 入栈
int my_push_stack(queue_t *A, queue_t *B, int num){
    if(NULL == A || NULL == B){
        return -1;
    }
    int temp = 0;
    if(is_empty(A)){
        push_queue(A, num);
        while(!pop_queue(B, &temp)){
            push_queue(A, temp);
        }
    }else{
        push_queue(B, num);
        while(!pop_queue(A, &temp)){
            push_queue(B, temp);
        }
    }
    return 0;
}

// 出栈
int my_pop_stack(queue_t *A, queue_t *B, int *buff){
    if(NULL == A || NULL == B || NULL == buff){
        return -1;
    }
    if(is_empty(A)){
        if(is_empty(B)){
            return -1;
        }else{
            pop_queue(B, buff);
        }
    }else{
        pop_queue(A, buff);
    }
    return 0;
}

int main(int argc, const char *argv[]){
    queue_t *A = NULL;
    create_queue(&A);
    queue_t *B = NULL;
    create_queue(&B);
    
    // 入栈
    my_push_stack(A, B, 10);
    my_push_stack(A, B, 20);
    // 出栈
    int value = 0;
    my_pop_stack(A, B, &value);
    printf("value = %d\n", value); // 20
    // 入栈
    my_push_stack(A, B, 30);
    my_push_stack(A, B, 40);
    // 出栈
    my_pop_stack(A, B, &value);
    printf("value = %d\n", value); // 40
    my_pop_stack(A, B, &value);
    printf("value = %d\n", value); // 30
    my_pop_stack(A, B, &value);
    printf("value = %d\n", value); // 10
    int ret = my_pop_stack(A, B, &value);
    printf("ret = %d\n", ret); // -1

    destroy_queue(&A);
    destroy_queue(&B);

    return 0;
}